<?php
class OfficesController extends AppController {

	var $name = 'Offices';
	var $uses= array('Affiliate','Office');
	function index() {
		$this->Office->recursive = 0;
		$this->paginate = array('order'=>'Office.id DESC','limit' =>20);
        $Offices = $this->paginate('Office');
		$this->set('Offices', $Offices);	

	}
	function add() {
		// lấy danh sách các chi nhánh
		$listBranch = $this->Affiliate->find('list', array(
														'fields' => array('Affiliate.id', 'Affiliate.name'),
														'recursive' => 0
														));
		$this->set('listBranch',$listBranch);
		if (!empty($this->data)) {
			if($this->data['Office']['id_affiliates']==''){
				$this->data['Offices']['id_affiliates']=0;
			}
		if($this->Office->save($this->data)){
			
			$this->redirect('index');
		}	
		}
	}
	
	function edit($id=null) {
			// lấy danh sách các chi nhánh
		$listBranch = $this->Affiliate->find('list', array(
														'fields' => array('Affiliate.id', 'Affiliate.name'),
														'recursive' => 0
														));
		$this->set('listBranch',$listBranch);
		if(!$id){
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name);			
		}else{
			if (!empty($this->data)) {			
			if ($this->Office->save($this->data)) {
				$this->Session->setFlash(__('Sửa thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không sửa được vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Office->read(null, $id);
		}
		}

	}

	
	function delete() {
		if($_POST['chkId']){
			$Idnews=$_POST['chkId'];
			foreach($Idnews as $id){
				$this->Office->delete($id);			
			}
			$this->Session->setFlash(__('Bạn Delete thành công', true));
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name);
		}
	}
	
}
?>